package com.svo.weitu.activity.fragment;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.svo.platform.widget.LoadDialog;
import com.svo.weitu.App;
import com.svo.weitu.R;
import com.svo.weitu.activity.WebViewActivity;
import com.svo.weitu.model.MNew;
import com.svo.weitu.model.push.MPush;

import cn.jpush.android.api.JPushInterface;


public class SetFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener,Preference.OnPreferenceChangeListener {

    private Preference check_update;
    //	private Preference advise;
    private Preference about;
    //	private Preference author;
    private CheckBoxPreference push_on_off;
    private CheckBoxPreference load_pic_on_off;
    private SharedPreferences settingSP;
    //	private EditTextPreference set_tag;
    private LoadDialog loadDialog;
    public SetFragment() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting);
        settingSP = PreferenceManager.getDefaultSharedPreferences(getActivity());
        check_update = findPreference("check_update");
//		advise = findPreference("advise");
        about = findPreference("about");
//		author = findPreference("author");
//		set_tag = (EditTextPreference) findPreference("set_tag");
        push_on_off = (CheckBoxPreference) findPreference("push_on_off");
        push_on_off.setChecked(settingSP.getBoolean("push_on_off", true));
        load_pic_on_off = (CheckBoxPreference) findPreference("load_pic_on_off");
        load_pic_on_off.setChecked(settingSP.getBoolean("load_pic_on_off", true));
        check_update.setOnPreferenceClickListener(this);
//		advise.setOnPreferenceClickListener(this);
        about.setOnPreferenceClickListener(this);
//		author.setOnPreferenceClickListener(this);
        push_on_off.setOnPreferenceClickListener(this);
        load_pic_on_off.setOnPreferenceClickListener(this);
//		set_tag.setOnPreferenceChangeListener(this);
//		set_tag.setOnPreferenceClickListener(this);
//		set_tag.setDialogMessage(Html.fromHtml("<font color='red' size='19px'>多个标签以逗号分隔</font>"));
    }
    @Override
    public boolean onPreferenceClick(Preference preference) {
        String key = preference.getKey();
        if ("check_update".equals(key)) {
            new MNew(getActivity()).checkNew(true,broadcastReceiver);
        }/*else if ("advise".equals(key)) {
			FeedbackAgent feedbackAgent = new FeedbackAgent(this);
			feedbackAgent.startFeedbackActivity();
		}*/else if ("about".equals(key)) {
            Intent intent = new Intent(getActivity(), WebViewActivity.class);
            intent.putExtra("title", "关于唯图");
            intent.putExtra("url", "file:///android_asset/about.html");
            startActivity(intent);
        }else if ("push_on_off".equals(key)) {
            if (push_on_off.isChecked()) {
                new MPush(App.context).init();
                JPushInterface.resumePush(App.context);
            }else {
                JPushInterface.stopPush(App.context);
            }
            settingSP.edit().putBoolean("push_on_off", push_on_off.isChecked()).commit();
        }else if ("load_pic_on_off".equals(key)) {
            if (load_pic_on_off.isChecked()) {
                ImageLoader.getInstance().denyNetworkDownloads(true);
            }else {
                ImageLoader.getInstance().denyNetworkDownloads(false);
            }
            settingSP.edit().putBoolean("load_pic_on_off", push_on_off.isChecked()).commit();
        }
        return true;
    }
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        String key = preference.getKey();
        if ("set_tag".equals(key)) {
        }
        return true;
    }

    private BroadcastReceiver broadcastReceiver;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (broadcastReceiver != null && getActivity() != null) {
            getActivity().unregisterReceiver(broadcastReceiver);
        }
    }
}
